package com.studymaster.controller.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.studymaster.domain.Event;
import com.studymaster.util.JsonHelper;

public class Convert {

	public static List<Event> getEventsByMap(String json){
		List<Event> events = new ArrayList<Event>();
		
		JSONObject jsonObject = null;
//		try {
//			jsonObject = new JSONObject(json);
//		} catch (JSONException ex) {
//			return events;
//		}
		
		JSONArray jsonArray = null;
		try{
			jsonArray = new JSONArray(json);
			
		}catch(JSONException e){
			return events;
		}
		
		Event event = null;
		for (int i=0;i<jsonArray.length();i++){
			
			
			try{
				jsonObject = jsonArray.getJSONObject(i);
				
			}catch(JSONException e){
				continue;
			}
			
			
			event = new Event();
			event.id 			= JsonHelper.getInt(jsonObject,"id");
			event.day 			= JsonHelper.getInt(jsonObject,"day");
			event.priority 		= JsonHelper.getInt(jsonObject,"priority");
			event.theme 		= JsonHelper.getInt(jsonObject,"day");
			event.timeline 		= JsonHelper.getInt(jsonObject,"timeline");
			event.studentid 	= JsonHelper.getString(jsonObject,"studentid");
			event.enddate 		= JsonHelper.getString(jsonObject,"enddate");
			event.startdate 	= JsonHelper.getString(jsonObject,"startdate");
			event.memo 			= JsonHelper.getString(jsonObject,"memo");
			event.site 			= JsonHelper.getString(jsonObject,"site");
			event.name 			= JsonHelper.getString(jsonObject,"name");
			event.role 			= JsonHelper.getString(jsonObject,"role");
			
			events.add(event);
		}
		
		
		return	events;
	}
}
